<?php

namespace Home\Controller;
use Common\Controller\BaseController;

class ContractController extends BaseController {

    public function index(){
    
        $contract_id = I('get.contract_id','','htmlspecialchars');
        $company_name = I('get.company_name','','htmlspecialchars');
        $make_time = strtotime(I('get.make_time','','htmlspecialchars'));
        $end_time = strtotime(I('get.end_time','','htmlspecialchars'));
        $now = time();
        $companyModel = D('Contract');
        $where = ' del=1 ';

        if($contract_id){
            $where  .= "and locate('{$contract_id}',contract_id) ";
        }
        if($company_name){
            $where  .= "and locate('{$company_name}',company_name) ";
        }
        if($make_time){
            $where  .= "and make_time>={$make_time} and make_time<{$now}";
        }
        if($end_time){
            $where  .= "and end_time>={$end_time} and end_time<{$now}";
        }

        $field = "contract.*";
        $list = $companyModel
            ->field($field)
//            ->join('user on company.user_id=user.user_id')
            ->where($where)
            ->order('reg_time DESC')
            ->select();

        $this->assign('list',$list);
        $this->assign('contract_id',$contract_id);
        $this->assign('company_name',$company_name);
        $this->assign('make_time',$make_time ? date('Y-m-d',$make_time) : '');
        $this->assign('end_time',$end_time ? date('Y-m-d',$end_time) : '');

        $this->display('index');
    }

    public function add(){

        $this->assign('now',date('Y-m-d',time()));
        $this->display('add');
    }

    public function edit(){

        $contract_id    = I('get.cid');
        $companyModel  = D('Contract');

        $contract = $companyModel->where('contract_id='.intval($contract_id))->limit(1)->select();

        if(empty($contract)){
            $this->error('维保合同信息不存在');
        }

        $roleModel = D('User');
        $user_list = $roleModel->getUserList(4);

        $this->assign('user_list',$user_list);
        $this->assign('contract',$contract[0]);
        $this->display('edit');
    }

    public function save(){

        $ret = array('status'=>200,'msg'=>'','content'=>'');

        if(IS_POST){
            try{

                $contractModel = D('Contract');
                $post      = I('post.');
                $res = $contractModel->saveContract($post);
                $ret['status'] = (false==$ret) ? 0 : 200;
                $ret['msg'] = $res['msg'];

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '请求不允许';
        }

        $this->ajaxReturn($ret);
    }
    public function delete(){

        $company_id    = I('get.cid');
        $companyModel  = D('Company');

        $company = $companyModel->where('company_id='.intval($company_id))->limit(1)->select();

        if(!empty($company)){
            try{
                $info = array('company_id'=>intval($company),'del'=>2);
                $res = $companyModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '企业不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $contract_id    = I('get.cid');
        $contractModel  = D('Contract');

        $field = "contract.*";
        $contract = $contractModel
            ->field($field)
//            ->join('user on company.user_id=user.user_id')
            ->where('contract_id='.intval($contract_id)." and del=1")
            ->limit(1)
            ->select();

        if(empty($contract)){
            $this->error('合同不存在');
        }

        $this->assign('contract',$contract[0]);
        $this->display('view');
    }

}